import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class RandomSelection {
    private static List<String> groups = new ArrayList<>(); // 存储小组的列表
    private static List<String> students = new ArrayList<>(); // 存储学生的列表
    private static List<String> classes = new ArrayList<>(); // 存储班级的列表
    private static List<String> scores = new ArrayList<>(); // 存储小组评分

    // 随机抽取小组
    public void randomGroupSelection() {
        // 实现随机抽取小组的逻辑
    }

    // 随机在小组中抽取学生
    public void randomStudentInGroup() {
        // 实现随机在小组中抽取学生的逻辑
    }

    // 随机在班级里抽取学生
    public void randomStudentInClass() {
        // 实现随机在班级里抽取学生的逻辑
    }

    // 瓮组评分
    public void groupScoring() {
        // 实现瓮组评分的逻辑
    }

    // 记录学生考勤、答题情况
    public void recordAttendanceAndAnswers() {
        // 实现记录学生考勤、答题情况的逻辑
    }

    // 导出成绩
    public void exportGrades() {
        // 实现导出成绩的逻辑
    }

    public static void main(String[] args) {
        // 添加一些示例数据
        groups.add("小组A");
        groups.add("小组B");
        students.add("学生A");
        students.add("学生B");
        classes.add("班级A");
        classes.add("班级B");

        SwingUtilities.invokeLater(() -> {
            createAndShowGUI();
        });
    }

    private static void createAndShowGUI() {
        JFrame frame = new JFrame("随机点名");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setSize(400, 300);

        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(0, 1));
        frame.getContentPane().add(panel);

        JLabel label = new JLabel("随机点名功能", JLabel.CENTER);
        panel.add(label);

        JButton randomGroupButton = new JButton("随机抽取小组");
        randomGroupButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Random random = new Random();
                String group = groups.get(random.nextInt(groups.size()));
                JOptionPane.showMessageDialog(frame, "随机抽取的小组: " + group);
            }
        });
        panel.add(randomGroupButton);

        JButton randomStudentInGroupButton = new JButton("随机在小组中抽取学生");
        randomStudentInGroupButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String selectedGroup = (String) JOptionPane.showInputDialog(frame, "选择小组:", "小组选择", JOptionPane.QUESTION_MESSAGE, null, groups.toArray(), groups.get(0));
                if (selectedGroup != null) {
                    // 从选择的小组中随机抽取学生
                    Random random = new Random();
                    String student = students.get(random.nextInt(students.size()));
                    JOptionPane.showMessageDialog(frame, "在小组 " + selectedGroup + " 中随机抽取的学生: " + student);
                }
            }
        });
        panel.add(randomStudentInGroupButton);

        JButton randomStudentInClassButton = new JButton("随机在班级里抽取学生");
        randomStudentInClassButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String selectedClass = (String) JOptionPane.showInputDialog(frame, "选择班级:", "班级选择", JOptionPane.QUESTION_MESSAGE, null, classes.toArray(), classes.get(0));
                if (selectedClass != null) {
                    Random random = new Random();
                    String student = students.get(random.nextInt(students.size()));
                    JOptionPane.showMessageDialog(frame, "在班级 " + selectedClass + " 中随机抽取的学生: " + student);
                }
            }
        });
        panel.add(randomStudentInClassButton);

        JButton scoreGroupButton = new JButton("给小组评分");
        scoreGroupButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String selectedGroup = (String) JOptionPane.showInputDialog(frame, "选择小组:", "小组选择", JOptionPane.QUESTION_MESSAGE, null, groups.toArray(), groups.get(0));
                if (selectedGroup != null) {
                    String score = JOptionPane.showInputDialog(frame, "请输入给小组 " + selectedGroup + " 的评分:");
                    if (score != null && !score.trim().isEmpty()) {
                        scores.add(selectedGroup + " 的评分: " + score);
                        JOptionPane.showMessageDialog(frame, "评分成功: " + selectedGroup + " 的评分为 " + score);
                    }
                }
            }
        });
        panel.add(scoreGroupButton);

        JButton recordAttendanceButton = new JButton("记录学生考勤");
        recordAttendanceButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String studentName = (String) JOptionPane.showInputDialog(frame, "选择学生:", "学生选择", JOptionPane.QUESTION_MESSAGE, null, students.toArray(), students.get(0));
                if (studentName != null) {
                    String attendance = JOptionPane.showInputDialog(frame, "请输入考勤状态 (到课/缺课):");
                    if (attendance != null && !attendance.trim().isEmpty()) {
                        JOptionPane.showMessageDialog(frame, "记录成功: " + studentName + " 的考勤状态为 " + attendance);
                    }
                }
            }
        });
        panel.add(recordAttendanceButton);

        JButton exportGradesButton = new JButton("导出成绩");
        exportGradesButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                StringBuilder gradeList = new StringBuilder();
                for (String score : scores) {
                    gradeList.append(score).append("\n");
                }
                JOptionPane.showMessageDialog(frame, gradeList.length() > 0 ? gradeList.toString() : "没有评分记录");
            }
        });
        panel.add(exportGradesButton);

        frame.setVisible(true);
    }
}
